{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-11-28T06:43:07.712300100Z",
     "start_time": "2023-11-28T06:43:07.702312Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import torch\n",
    "from torchvision import datasets,transforms\n",
    "import torch.optim as optim\n",
    "import torchvision\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "class LeNet(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.c1 = nn.Conv2d(1,6,(5,5))\n",
    "        self.c3 = nn.Conv2d(6,16,5)\n",
    "        self.fc1 = nn.Linear(16*4*4,120)\n",
    "        self.fc2 = nn.Linear(120,84)\n",
    "        self.fc3 = nn.Linear(84,10)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(F.relu(self.c1(x)),2)\n",
    "        out = F.max_pool2d(F.relu(self.c3(out)),2)\n",
    "        out = out.view(-1,16*4*4)\n",
    "        out = F.relu(self.fc1(out))\n",
    "        out = F.relu(self.fc2(out))\n",
    "        out = self.fc3(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-28T06:02:13.566296300Z",
     "start_time": "2023-11-28T06:02:13.559313200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 准备数据"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "data_path = '../data'\n",
    "trainset = datasets.MNIST(data_path,train=True,download=False,transform=transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize((0.1307),(0.3081))\n",
    "]))\n",
    "testset = datasets.MNIST(data_path,train=False,download=False,transform=transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize((0.1307),(0.3081))\n",
    "]))\n",
    "\n",
    "train_loader = torch.utils.data.DataLoader(trainset,batch_size=4,shuffle=True,num_workers=2)\n",
    "test_loader = torch.utils.data.DataLoader(testset,batch_size=4,shuffle=True,num_workers=2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-28T06:12:47.355814500Z",
     "start_time": "2023-11-28T06:12:44.301926100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "import datetime\n",
    "def train__loop(models,n_epochs,loss_fn,optimizer,train_loader):\n",
    "    for epoch in range(1,n_epochs+1):\n",
    "        loss_train = 0.0\n",
    "        for i,data in enumerate(train_loader,1):\n",
    "            imgs,labels = data\n",
    "            imgs,labels = imgs.cuda(),labels.cuda()\n",
    "            outputs = models(imgs)\n",
    "            loss = loss_fn(outputs,labels)\n",
    "            optimizer.zero_grad()\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "            loss_train+=loss.item()\n",
    "            if i%1000 ==0:\n",
    "                print('{}, Epoch:{}, i:{}, loss_train:{}'.format(datetime.datetime.now(),epoch,i,loss_train/1000))\n",
    "                loss_train = 0.0\n",
    "    print('训练结束')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-28T06:24:38.818106600Z",
     "start_time": "2023-11-28T06:24:38.796105800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-11-28 14:31:44.252309, Epoch:1, i:1000, loss_train:1.2924928033724428\n",
      "2023-11-28 14:31:48.084309, Epoch:1, i:2000, loss_train:0.30447081164183326\n",
      "2023-11-28 14:31:51.928306, Epoch:1, i:3000, loss_train:0.215288390825066\n",
      "2023-11-28 14:31:56.046224, Epoch:1, i:4000, loss_train:0.1726494760189089\n",
      "2023-11-28 14:32:00.286223, Epoch:1, i:5000, loss_train:0.14369394886920417\n",
      "2023-11-28 14:32:04.467223, Epoch:1, i:6000, loss_train:0.12951954837723678\n",
      "2023-11-28 14:32:08.415238, Epoch:1, i:7000, loss_train:0.12156988196531893\n",
      "2023-11-28 14:32:12.142224, Epoch:1, i:8000, loss_train:0.0977823515772543\n",
      "2023-11-28 14:32:15.996223, Epoch:1, i:9000, loss_train:0.10070469579442488\n",
      "2023-11-28 14:32:20.469227, Epoch:1, i:10000, loss_train:0.0899933060953681\n",
      "2023-11-28 14:32:24.370183, Epoch:1, i:11000, loss_train:0.09451617734537467\n",
      "2023-11-28 14:32:28.489185, Epoch:1, i:12000, loss_train:0.09303074381771693\n",
      "2023-11-28 14:32:32.543183, Epoch:1, i:13000, loss_train:0.07386734120865049\n",
      "2023-11-28 14:32:36.607098, Epoch:1, i:14000, loss_train:0.08144400468227286\n",
      "2023-11-28 14:32:40.292097, Epoch:1, i:15000, loss_train:0.08258707475711889\n",
      "训练结束\n"
     ]
    }
   ],
   "source": [
    "lenet = LeNet()\n",
    "optimizer = optim.SGD(lenet.parameters(),lr=0.001,momentum=0.9)\n",
    "loss_fn = nn.CrossEntropyLoss()\n",
    "train__loop(models=lenet.cuda(),n_epochs=1,loss_fn=loss_fn,optimizer=optimizer,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-28T06:32:42.764416300Z",
     "start_time": "2023-11-28T06:30:46.563973800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "def test_loop(models,test_loader):\n",
    "    correct=0\n",
    "    total = 0\n",
    "    with torch.no_grad():\n",
    "        for imgs,labels in test_loader:\n",
    "            imgs,labels = imgs.cuda(),labels.cuda()\n",
    "            outputs = models(imgs)\n",
    "            _,preds = torch.max(outputs,dim=1)\n",
    "            correct += int((labels==preds).sum())\n",
    "            total += labels.shape[0]\n",
    "    print('精度:{:.3f}%'.format(correct/total*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-28T06:41:09.727275900Z",
     "start_time": "2023-11-28T06:41:09.700282300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "精度:98.060%\n"
     ]
    }
   ],
   "source": [
    "test_loop(models=lenet.cuda().eval(),test_loader=test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-28T06:41:49.790023500Z",
     "start_time": "2023-11-28T06:41:37.900280500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "def imshow(inputs):\n",
    "    inputs = inputs/2+0.5\n",
    "    inputs = inputs.numpy().transpose(1,2,0)\n",
    "    plt.imshow(inputs)\n",
    "    plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-28T06:43:37.317028500Z",
     "start_time": "2023-11-28T06:43:37.238009600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).\n"
     ]
    },
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAiYAAACxCAYAAADwMnaUAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8pXeV/AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAaB0lEQVR4nO3de3RU1dnH8d9wG5IYohCZEAMYVlPRIhQSoCAVsJKWm6V0tQIKWFvlLoG1gABtiVSTSCtCFbCyCthahKqgSJESBIOUWiAQroK6iBCUmIqQhNskkv3+wcvUM4GQyZkkJ8n3s9b88Zyzz56HB0we9+w5x2WMMQIAAHCABjWdAAAAwFU0JgAAwDFoTAAAgGP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bqyYAAAAxwioMUlLS1PXrl0VHh6uli1basiQITp69KhljDFGKSkpio6OVkhIiPr06aNDhw4FNWkAAFA3BdSYZGZmasKECfrggw+UkZGhr7/+WomJiTp//rxvzLx58zR//ny98MIL2rVrl6KiotSvX796e2tdAABQcQHtMdm4caMlXr58uVq2bKmsrCzde++9MsZowYIFmj17toYOHSrpymfqHo9HK1eu1JgxY4KXucPc6DNj/z0he/bsqcp0ykhISLDEu3fvrtb3r69GjRoV1PnYmxC4mt5PgvrjySefDGj80qVLqyiT2s3WHpOCggJJUvPmzSVJOTk5ysvLU2Jiom+M2+1W7969tWPHDjtvBQAA6oFKfyvHGKOpU6eqV69e6tChgyQpLy9PkuTxeCxjPR7Pde9c6PV65fV6fXFhYWFlUwIAALVcpVdMJk6cqP3791/z1s0ul8sSG2PKHLsqLS1NERERvlfr1q0rmxIAAKjlKrViMmnSJK1bt07btm1TTEyM73hUVJSkKysnrVq18h3Pz88vs4py1cyZMzV16lRfXFhYWCebk+reU+Jv7ty5lpi9ClWjb9++lnjYsGG25hsxYoSt6xEcK1eutMSB/r3861//CmY6cIjf/OY3AY1PTU2tokzqloBWTIwxmjhxotasWaMtW7YoNjbWcj42NlZRUVHKyMjwHSsuLlZmZqZ69ux5zTndbreaNWtmeQEAgPopoBWTCRMmaOXKlXrrrbcUHh7u21MSERGhkJAQuVwuJSUlKTU1VXFxcYqLi1NqaqpCQ0P5Pz8AAHBDATUmS5YskST16dPHcnz58uV65JFHJF25tfrFixc1fvx4nTlzRt27d9emTZsUHh4elIQBAEDdFVBjYoy54RiXy6WUlJQyz5IB6qKGDRta4mnTptmab9myZZaYZ+HYd639VIE+28ruiu9nn31m63o4Q1xcnCXu0aNHuePPnTtnibdv3x70nOoinpUDAAAcg8YEAAA4Bo0JAABwjErf+RU168iRI7aub9GiRZljp0+ftjVnffT222/bun7dunWW+PXXX7c1HyrGf9/JNx+jIUlJSUm25ve/0/WKFStszQdnWLhwYUDjv3mPLlQcKyYAAMAxaEwAAI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GRkZvnPFxcXKzMxUz5497b4NAACoBwJaMZk1a5b69++v1q1bq6ioSKtWrdJ7772njRs3yuVyKSkpSampqYqLi1NcXJxSU1MVGhqqESNGVFX+AACgDgmoMfniiy80cuRInTp1ShEREerYsaM2btyofv36SZKmT5+uixcvavz48Tpz5oy6d++uTZs2BfSo6avfXvZ6vYGkBgAAatDV39s270Ji/z4mwXby5Em+mQMAQC2Vm5urmJiYSl/vuMaktLRUn3/+ucLDw1VUVKTWrVsrNzfX1s1a6rPCwkJqaBM1tI8aBgd1tI8a2ne9GhpjVFRUpOjoaDVoUPktrI57unCDBg18ndbVrxlffTYPKo8a2kcN7aOGwUEd7aOG9l2rhhEREbbn5enCAADAMWhMAACAYzi6MXG73ZozZw43YLOBGtpHDe2jhsFBHe2jhvZVdQ0dt/kVAADUX45eMQEAAPULjQkAAHAMGhMAAOAYNCYAAMAxHNuYLF68WLGxsWratKni4+P1/vvv13RKjpWWlqauXbsqPDxcLVu21JAhQ3T06FHLGGOMUlJSFB0drZCQEPXp00eHDh2qoYydLy0tzfdgyquoYcV89tlnevjhh9WiRQuFhobqu9/9rrKysnznqWP5vv76a/36179WbGysQkJC1K5dO82dO1elpaW+MdTQatu2bRo8eLCio6Plcrn05ptvWs5XpF5er1eTJk1SZGSkwsLC9MADD+jkyZPV+KeoeeXVsaSkRDNmzNDdd9+tsLAwRUdHa9SoUfr8888tcwSljsaBVq1aZRo3bmyWLl1qDh8+bCZPnmzCwsLM8ePHazo1R/rhD39oli9fbg4ePGiys7PNwIEDTZs2bcy5c+d8Y9LT0014eLh54403zIEDB8yDDz5oWrVqZQoLC2swc2fauXOnuf32203Hjh3N5MmTfcep4Y199dVXpm3btuaRRx4x//nPf0xOTo7ZvHmz+eSTT3xjqGP5nnrqKdOiRQuzfv16k5OTY1577TVz0003mQULFvjGUEOrDRs2mNmzZ5s33njDSDJr1661nK9IvcaOHWtuu+02k5GRYfbs2WP69u1rOnXqZL7++utq/tPUnPLqePbsWXP//feb1atXmyN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GRobleEZGhnr27FlDWTmbMUYTJ07UmjVrtGXLFsXGxlrOx8bGKioqylLT4uJiZWZmUtP/94Mf/EAHDhxQdna275WQkKCHHnpI2dnZateuHTWsgHvuuafMV9U/+ugjtW3bVhL/FiviwoULatDA+qO5YcOGvq8LU8PAVKRe8fHxaty4sWXMqVOndPDgQWr6DVebko8//libN29WixYtLOeDVscANulWm6tfF/7zn/9sDh8+bJKSkkxYWJj59NNPazo1Rxo3bpyJiIgw7733njl16pTvdeHCBd+Y9PR0ExERYdasWWMOHDhghg8fXq+/XlgR3/xWjjHUsCJ27txpGjVqZJ5++mnz8ccfm7/97W8mNDTUvPLKK74x1LF8o0ePNrfddpvv68Jr1qwxkZGRZvr06b4x1NCqqKjI7N271+zdu9dIMvPnzzd79+71fVukIvUaO3asiYmJMZs3bzZ79uwx9913X737unB5dSwpKTEPPPCAiYmJMdnZ2ZbfNV6v1zdHMOroyMbEGGMWLVpk2rZta5o0aWK6dOni++orypJ0zdfy5ct9Y0pLS82cOXNMVFSUcbvd5t577zUHDhyouaRrAf/GhBpWzNtvv206dOhg3G63ad++vXnppZcs56lj+QoLC83kyZNNmzZtTNOmTU27du3M7NmzLT/8qaHV1q1br/kzcPTo0caYitXr4sWLZuLEiaZ58+YmJCTEDBo0yJw4caIG/jQ1p7w65uTkXPd3zdatW31zBKOOLmOMCXQ5BwAAoCo4bo8JAACov2hMAACAY9CYAAAAx6AxAQAAjkFjAgAAHIPGBAAAOAaNCQAAcAwaEwAA4Bg0JgAAwDFoTAAAgGPQmAAAAMegMQEAAI7xf5opLxogj08cAAAAAElFTkSuQmCC"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测值---测试值\n",
      "tensor(9) --- tensor(9, device='cuda:0')\n",
      "tensor(7) --- tensor(7, device='cuda:0')\n",
      "tensor(8) --- tensor(8, device='cuda:0')\n",
      "tensor(7) --- tensor(7, device='cuda:0')\n"
     ]
    }
   ],
   "source": [
    "inputs,labels = next(iter(test_loader))\n",
    "imshow(torchvision.utils.make_grid(inputs))\n",
    "outputs = lenet.cuda().eval()(inputs.cuda())\n",
    "_,preds= torch.max(outputs,dim=1)\n",
    "print('预测值---测试值')\n",
    "for i,j in zip(labels,preds):\n",
    "    print(i,'---',j)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-28T06:48:42.879014300Z",
     "start_time": "2023-11-28T06:48:36.090227200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
